﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Brain.Logic.AI
{
    public class ArtificialIntelligence : IUpdateable, IArtificialIntelligence
    {
        /// <summary>
        /// The Current Status
        /// </summary>
        public Enum CurrentStatus { get; protected set; }
        public IAIAction CurrentAction { get; protected set; }

        public bool ShouldUpdate { get; set; }

        public Vector3 Position { get; set; }

        public object Tag1 { get; set; }
        public object Tag2 { get; set; }
        public object Tag3 { get; set; }

        public IAIData Data { get; set; }

        public AIInfo Info { get; set; }

        public ArtificialIntelligence(AIInfo info)
        {
            this.Info = info;
            Engine.Instance.AIManager.AddAI(this);
            ShouldUpdate = true;
        }

        public virtual void Look(IArtificialIntelligence anotherAI)
        {
            if (CurrentAction != null)
            {
                CurrentAction.Look(this, anotherAI);
            }
        }

        public virtual void SwitchStatus(Enum status)
        {
            if (Info.Actions.ContainsKey(status))
            {
                this.CurrentStatus = status;
                this.CurrentAction = Info.Actions[status];
            }
        }

        public virtual void Update()
        {
            if (Data != null)
            {
                if (Data.ShouldUpdate)
                {
                    Data.Update();
                }
            }
            if (CurrentAction != null)
            {
                CurrentAction.Update(this);
            }

            if (Tag1 is IUpdateable)
            {
                IUpdateable tag1 = Tag1 as IUpdateable;
                if (tag1.ShouldUpdate)
                {
                    tag1.Update();
                }
            }
            if (Tag2 is IUpdateable)
            {
                IUpdateable tag2 = Tag2 as IUpdateable;
                if (tag2.ShouldUpdate)
                {
                    tag2.Update();
                }
            }
            if (Tag3 is IUpdateable)
            {
                IUpdateable tag3 = Tag3 as IUpdateable;
                if (tag3.ShouldUpdate)
                {
                    tag3.Update();
                }
            }
        }

    }
}
